﻿using System;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using mojoPortal.Web.RS.PRM.AppCode.Lib;

namespace mojoPortal.Web.RS.PRM.Controls
{
    public partial class ChargeControl : RSBaseUserControl
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
            }
        }

        public void LoadPropertyChargeHistory(int companyid, int propertyid, int leaseid)
        {
            hfLeaseID.Value = leaseid.ToString();
            hfPropertyID.Value = propertyid.ToString();
            LoadPropertyCharges(companyid, propertyid, leaseid);
        }


        //Begin Charge History

        private bool LoadPropertyCharges(int companyid, int propertyid, int leaseid)
        {
            bool bHasValue = false;
            if (companyid > 0)
            {
                RSPRMDataLookupLoader.LoadCodeDDL(companyid, "CHARGE_CAT", ddlChargeCode);
                RSPRMDataLookupLoader.LoadTypeDDL(companyid, "CHARGE_CAT", ddlChargeType);
                SqlCommand myCommand;
                SqlParameter param;
                SqlConnection myConnection;
                String sqlSELECT = "SELECT c.ChargeID,c.ChargeCodeID,c.ChargeTypeID,(SELECT t.TypeName FROM rs_tblType t WHERE t.TypeId=c.ChargeTypeID AND t.CompanyId=c.CompanyID) AS ChargeType, (SELECT d.CodeName FROM rs_tblCode d WHERE d.CodeId=c.ChargeCodeID AND d.CompanyId=c.CompanyID) AS ChargeCode, c.ChargeAmount,c.ChargeDate,c.ChargeDescription,c.Balance,c.LeaseID, " +
                                    "c.PropertyID,c.CompanyID,c.Audit_CreateDt,c.Audit_CreateBy FROM rs_tblCharge c " +
                                    "WHERE c.LeaseID=@LeaseID AND c.PropertyID=@PropertyID AND c.CompanyID=@CompanyID ORDER BY c.ChargeDate DESC";

                myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
                myCommand = new SqlCommand(sqlSELECT, myConnection);

                param = new SqlParameter();
                param.SqlDbType = SqlDbType.Int;
                param.ParameterName = "@companyid";
                param.Value = companyid;
                myCommand.Parameters.Add(param);

                param = new SqlParameter();
                param.SqlDbType = SqlDbType.Int;
                param.ParameterName = "@propertyid";
                param.Value = propertyid;
                myCommand.Parameters.Add(param);

                param = new SqlParameter();
                param.SqlDbType = SqlDbType.Int;
                param.ParameterName = "@leaseid";
                param.Value = leaseid;
                myCommand.Parameters.Add(param);

                SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
                myConnection.Open();
                DataSet ds = new DataSet();
                adapter.Fill(ds);

                gvChargeHistory.DataSource = ds;
                gvChargeHistory.DataBind();
                myConnection.Close();

                if (gvChargeHistory.Rows.Count > 0)
                {
                    bHasValue = true;
                }
            }
            return bHasValue;
        }


        protected void ddlChargeType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlChargeType.SelectedValue != "0")
            {
                btnAddCharge.Enabled = true;
            }
            else
            {
                btnAddCharge.Enabled = false;
            }
        }

        protected void btnAddCharge_Click(object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite)
            {
                int companyid = userIdentity.RSCompanyId;
                int propertyid = int.Parse(hfPropertyID.Value);
                int leaseid = int.Parse(hfLeaseID.Value);
                int chargecodeid = int.Parse(ddlChargeCode.SelectedValue);
                int chargetypeid = int.Parse(ddlChargeType.SelectedValue);
                string chargedescription = txtChargeDescription.Text;
                string chargeamount = txtChargeAmount.Text;
                string chargedate = txtChargeDate.Text;

                if (chargedescription.Length > 0)
                {
                    SqlCommand sqlCmd = new SqlCommand();
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@PropertyID", SqlDbType.Int, 0, ParameterDirection.Input, propertyid);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@LeaseID", SqlDbType.Int, 0, ParameterDirection.Input, leaseid);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@ChargeCodeID", SqlDbType.Int, 0, ParameterDirection.Input, chargecodeid);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@ChargeTypeID", SqlDbType.Int, 0, ParameterDirection.Input, chargetypeid);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@ChargeDescription", SqlDbType.NVarChar, 250, ParameterDirection.Input, chargedescription);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@ChargeAmount", SqlDbType.Float, 0, ParameterDirection.Input, float.Parse(chargeamount));
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@ChargeDate", SqlDbType.DateTime, 0, ParameterDirection.Input, Convert.ToDateTime(chargedate));
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@Companyid", SqlDbType.Int, 0, ParameterDirection.Input, companyid);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@CurrentUser", SqlDbType.NVarChar, 15, ParameterDirection.Input, userIdentity.RSCurrentUser);
                    SQLHelper.SetCommandType(sqlCmd, CommandType.StoredProcedure, "rs_uspSaveCharge");
                    SQLHelper.ExecuteScalarCmd(sqlCmd);
                    int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
                    LoadPropertyChargeHistory(companyid, propertyid, leaseid);
                }
            }
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);
        }

        protected void gvCharge_Remove(object sender, CommandEventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            int chargeid = Convert.ToInt32(e.CommandArgument.ToString());
            if (chargeid > 0)
            {
                int propertyid = int.Parse(hfPropertyID.Value);
                int leaseid = int.Parse(hfLeaseID.Value);
                SqlCommand sqlCmd = new SqlCommand();
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@ChargeID", SqlDbType.Int, 0, ParameterDirection.Input, chargeid);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@Companyid", SqlDbType.Int, 0, ParameterDirection.Input, userIdentity.RSCompanyId);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@CurrentUser", SqlDbType.NVarChar, 15, ParameterDirection.Input, userIdentity.RSCurrentUser);
                SQLHelper.SetCommandType(sqlCmd, CommandType.Text, "DELETE FROM rs_tblCharge WHERE ChargeID=@chargeid AND CompanyID=@CompanyID");
                SQLHelper.ExecuteScalarCmd(sqlCmd);
                int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
                LoadPropertyChargeHistory(userIdentity.RSCompanyId, propertyid, leaseid);
            }
        }

        //End of ChargeHistory
        private string RedirectToCurrentURL(string activetab)
        {
            var nameValues = HttpUtility.ParseQueryString(Request.QueryString.ToString());
            nameValues.Set("selindex", activetab);
            string url = Request.Url.AbsolutePath;
            string updatedQueryString = "?" + nameValues.ToString();
            url = url + updatedQueryString;
            return url;
        }
    }
}